package com.zhku.laosiji.controller;

import com.zhku.laosiji.pojo.Orders;
import com.zhku.laosiji.pojo.User;
import com.zhku.laosiji.service.OrderService;
import com.zhku.laosiji.service.ParkService;
import com.zhku.laosiji.utils.KeyUtil;
import com.zhku.laosiji.vo.OrderVo;
import com.zhku.laosiji.vo.OrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Jiayuan
 * @version 1.0
 * @description:
 * @time 9/11/2018 2:56 PM
 */

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ParkService parkService;

    //用户下单一个车位，并查询该用户所有订单列表
    @GetMapping("/findAll")
    public ModelAndView findAll(Map<String,Object> map,HttpSession session){
        if(session.getAttribute("user") != null) {
            //Integer parkId = Integer.parseInt(parkIdStr);
            Integer userId =((User)session.getAttribute("user")).getId();
            //查询列表
            List<OrderVo> orders = orderService.findAll(userId);
            map.put("orders",orders);
            map.put("user",((User)session.getAttribute("user")));
            return new ModelAndView("/user/car/order",map);
        }else{
            return new ModelAndView("/user/join");
        }
    }

    @RequestMapping("/newOrder")
    public ModelAndView newOrder(Map<String,Object> map,Integer parkId,HttpSession session){
        if(session.getAttribute("user") != null) {
            //获取当前登录用户信息
            Integer userId = ((User)session.getAttribute("user")).getId();
            if(!(parkService.findDetail(parkId).getStatus() == 1)){
                //创建一个订单对象
                Orders order = new Orders();
                order.setCode(KeyUtil.getUniqueKey());
                order.setUserId(userId);
                order.setParkId(parkId);
                order.setCreatedate(new Date());
                order.setStatus(0);
                order.setTotal(parkService.findDetail(parkId).getPrice());
                //下单一个车位
                int insert = orderService.insert(order);
                //将车位设置为被占用
                int status = 1;
                parkService.updateStatus(parkId,status);
            }
            //查询列表,按下单顺序
            List<OrderVo> orders = orderService.findAll(userId);
            map.put("orders",orders);
            map.put("user",((User)session.getAttribute("user")));
            return new ModelAndView("/user/car/order",map);
        }else{
            return new ModelAndView("/user/join");
        }
    }

    //完结订单
    @RequestMapping("/finish")
    public String finish(String code){
        Orders orders = orderService.selectByOrderCode(code);
        orders.setStatus(1);
        Integer finish = orderService.finish(orders);
        //归还车位
        int status = 0;
        parkService.updateStatus(orders.getParkId(),status);
        return "redirect:/order/findAll";
            //return new ModelAndView("/user/car/order");
        }

    //删除订单
    @RequestMapping("delete")
    public String delete(String code){
        orderService.deleteByCode(code);
        return "redirect:/order/findAll";
    }

	
	@RequestMapping("/list")
	public ModelAndView selectAllOrder() {
		List<OrdersVO> allOrders = orderService.selectAllOrder();
		Integer totalOrders = orderService.countTotalOrder();
		ModelAndView modelAndView = new ModelAndView("/order-list");
		modelAndView.addObject("allOrders", allOrders);
		modelAndView.addObject("totalOrders", totalOrders);
		return modelAndView;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String delOrder(Integer id) {
		orderService.deleteByPrimaryKey(id);
		return "OK";
	}
	
	@RequestMapping("/membershow")
	public ModelAndView toMemberShow() {
		return new ModelAndView("/member-show");
	}
}

